package com.sgb.kjwl.view.ui.activity.manage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.entity.LocalMedia;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AtWebsitUpDataForYejiBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.swgk.core.util.FileUtil;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.website.FileInfoEntity;
import com.sgb.kjwl.model.entity.website.WebsiteUpDataJingyingYejiEntity;
import com.sgb.kjwl.view.ui.adapter.manage.AddViewAdapter;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.viewmodel.manage.WebsiteUpDataJingYingYeJiModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author 李伟
 * @date 2021/5/17 14:19
 * @Description 网站发布，经营业绩发布功能界面
 * @name com.sgb.kjwl.view.ui.activity.manage
 */
public class WebsiteUpDataJingYingYeJiActivity extends WebSiteBuildBaseActivity implements ItemInputTextView.OnWholeItemClickListener {

    private AtWebsitUpDataForYejiBinding binding;
    @Inject
    WebsiteUpDataJingYingYeJiModel viewModel;


    private int flag = 0; // 切换业绩类型 0 工程项目、1商品供应、2租赁业绩、3物流业绩、4其他业绩

    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private AddViewAdapter mAddViewAdapter;

    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();

    private boolean isLoaded = false;
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private int cityPosition1_s;
    private int cityPosition2_s;
    private int cityPosition3_s;
    private int cityPosition1_e;
    private int cityPosition2_e;
    private int cityPosition3_e;

    private String filePath = "";
    private int siteType = 1; //网站类型  0-个人 1-企业 2-团队 默认为1
    private boolean isModify = false;
    /**
     * @param context
     * @param id      网站编号
     */
    public static void start(Context context, String id) {
        //网站类型  0-个人 1-企业 2-团队 默认为1
        int siteType = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1);
        start(context, id, siteType);
    }

    /**
     * @param context
     * @param id       网站编号
     * @param siteType 网站类型  0-个人 1-企业 2-团队 默认为1
     */
    public static void start(Context context, String id, int siteType) {
        Intent intent = new Intent(context, WebsiteUpDataJingYingYeJiActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("siteType", siteType);
        context.startActivity(intent);
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent).
                enterpriseManageModule(new EnterpriseManageModule(this)).
                build().inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        return false;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.at_websit_up_data_for_yeji);
        binding.setViewModel(viewModel.entity.get());
        binding.setFlag(flag);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("经营业绩");
        viewModel.entity.get().setType(1);
        binding.tvYeJiType.setOnWholeItemClickListener(this);
        binding.tvStatus.setOnWholeItemClickListener(this);
        binding.tvArea.setOnWholeItemClickListener(this);
        binding.tvUpload.setOnClickListener(this);
        binding.tvBtnEdit.setOnClickListener(this);
        binding.tvFileDelete.setOnClickListener(this);
        binding.tvStartAddr.setOnWholeItemClickListener(this);
        binding.tvEndAddr.setOnWholeItemClickListener(this);
        siteType = getIntent().getIntExtra("siteType", 1);

        binding.etName0.setInputType(InputType.TYPE_CLASS_TEXT);
        binding.etName1.setInputType(8194); //  数字
        binding.etLeadPhone.setInputType(8194);
        binding.etName1.setCheck(10, 2, true); //  输入限制，整数位10位保留两位小数
        binding.etName2.setInputType(InputType.TYPE_CLASS_TEXT);
        binding.etName3.setInputType(InputType.TYPE_CLASS_TEXT);
        binding.etName4.setInputType(InputType.TYPE_CLASS_TEXT);
        binding.etProduceNum.setInputType(8194);//产品数量
        binding.etPayTime.setInputType(8194);//交付及时率
        binding.etProduceQualifiedNum.setInputType(8194);//合格产品数量
        binding.etProduceNotQualifiedNum.setInputType(8194);//进场件不良率
        binding.etAdd.setInputType(8194);//同比增加
        binding.etProfits.setInputType(8194);//净利润
        binding.etDunNumber.setInputType(8194);//吨

        binding.etChengBao.setInputType(InputType.TYPE_CLASS_TEXT);//承包方式

        mAddViewAdapter = new AddViewAdapter(this);
        binding.addView.setAdapter(mAddViewAdapter);
    }

    @Override
    protected void init() {
        String id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            viewModel.updateWebsiteJYData(id);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_upload:
                pictureSelector(true, 500, 1);
                break;
            case R.id.tv_file_delete:
                filePath = "";
                binding.linBtnUpload.setVisibility(View.VISIBLE);
                binding.fileLayout.setVisibility(View.GONE);
                viewModel.entity.get().setFile("");
                break;
            case R.id.tv_btn_edit:
                if (!ClickUtils.isDoubleClick()) {
                    if (isInputValid()) {
                        if (!TextUtils.isEmpty(filePath) && !filePath.startsWith("http")) {
                            viewModel.uploadAvatarFileModel(new File(filePath), 1);
                            return;
                        }
                        if (TextUtils.isEmpty(viewModel.entity.get().getFile())) {
                            MToast.showToast(this, "请添加证明文件");
                            return;
                        }
                        viewModel.updateWebsiteJYData();
                    }
                }
                break;
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ye_ji_type:
                if(!isModify){
                    viewModel.getYejiType();
                }
                break;
            case R.id.tv_status:
                viewModel.getStatus();
                break;
            case R.id.tv_area:
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.tv_start_addr:
                if (addressEntityList != null) {
                    showCityPop1();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.tv_end_addr:
                if (addressEntityList != null) {
                    showCityPop2();
                } else {
                    viewModel.getAddressModel();
                }
                break;
        }
    }

    private boolean isInputValid() {
        if (TextUtils.isEmpty(viewModel.entity.get().getStatus())) {
            MToast.showToast(this, "请选择项目状态");
            return false;
        }
        if (TextUtils.isEmpty(binding.etName0.getContent().trim())) {
            MToast.showToast(this, binding.etName0.getHint());
            return false;
        }

        if (TextUtils.isEmpty(binding.etName4.getContent().trim())) {
            MToast.showToast(this, "请输入业主单位名称");
            return false;
        }
        if (TextUtils.isEmpty(binding.etName1.getContent().trim())) {
            MToast.showToast(this, "请输入项目金额");
            return false;
        }

        if (flag == 0) {
            if (TextUtils.isEmpty(binding.etChengBao.getContent().trim())) {
                MToast.showToast(this, "请输入承包方式");
                return false;
            }
        }


        if (TextUtils.isEmpty(binding.etName3.getContent().trim())) {
            MToast.showToast(this, "请输入甲方证明人名称");
            return false;
        }

        if (TextUtils.isEmpty(viewModel.entity.get().getPhone())) {
            MToast.showToast(this, "请输入联系电话");
            return false;
        }

        if (viewModel.entity.get().getPhone().length() != 11) {
            MToast.showToast(this, "请输入正确的联系电话");
            return false;
        }

        if (flag == 0) {
            if (TextUtils.isEmpty(binding.etName2.getContent().trim())) {
                MToast.showToast(this, "请输入参与人员名称");
                return false;
            }
            if (TextUtils.isEmpty(binding.etIntroduction.getEditContent().trim())) {
                MToast.showToast(this, "请输入项目简介信息");
                return false;
            }


        }

        if (flag == 1) {
            if (TextUtils.isEmpty(binding.etProduceNum.getContent().trim())) {
                MToast.showToast(this, "请输入产品供应数量");
                return false;
            }
            if (TextUtils.isEmpty(binding.etPayTime.getContent().trim())) {
                MToast.showToast(this, "请输入交付及时率");
                return false;
            }
            if (TextUtils.isEmpty(binding.etProduceQualifiedNum.getContent().trim())) {
                MToast.showToast(this, "请输入合格产品数量");
                return false;
            }
            if (TextUtils.isEmpty(binding.etProduceNotQualifiedNum.getContent().trim())) {
                MToast.showToast(this, "请输入进厂件不良率");
                return false;
            }
        }

        if (flag == 2) {
            if (TextUtils.isEmpty(binding.etAdd.getContent().trim())) {
                MToast.showToast(this, "请输入同比增加");
                return false;
            }
            if (TextUtils.isEmpty(binding.etProfits.getContent().trim())) {
                MToast.showToast(this, "请输入净利润");
                return false;
            }
        }

        if (flag == 3) {
            if (TextUtils.isEmpty(binding.etDunNumber.getContent().trim())) {
                MToast.showToast(this, "请输入载重");
                return false;
            }
        }

        viewModel.entity.get().setName(binding.etName0.getContent().trim());
        viewModel.entity.get().setPrice(binding.etName1.getContent().trim());
        viewModel.entity.get().setParticipant(binding.etName2.getContent().trim());
        viewModel.entity.get().setCertifier(binding.etName3.getContent().trim());
        viewModel.entity.get().setOwner(binding.etName4.getContent().trim());


        viewModel.entity.get().setSiteType(siteType + "");
        viewModel.entity.get().setCustomFields(binding.addView.getDate());
        if (flag == 0) {
            viewModel.entity.get().setContractMode(binding.etChengBao.getContent().trim());
            viewModel.entity.get().setDetailedAddress(binding.etAddressDetail.getEditContent().trim());
            viewModel.entity.get().setIntroduction(binding.etIntroduction.getEditContent().trim());
        }

        if (flag == 1) {
            viewModel.entity.get().setSupplyNum(binding.etProduceNum.getContent().trim());
            viewModel.entity.get().setQualifiedNum(binding.etProduceQualifiedNum.getContent().trim());
            viewModel.entity.get().setSupplyNum(binding.etPayTime.getContent().trim());
            viewModel.entity.get().setSupplyNum(binding.etProduceNotQualifiedNum.getContent().trim());
            viewModel.entity.get().setIntroduction(binding.etMiaoshuBeiZhu.getEditContent().trim());
        }
        if (flag == 2) {
            viewModel.entity.get().setIncrease(binding.etAdd.getContent().trim());
            viewModel.entity.get().setNetProfit(binding.etProfits.getContent().trim());
            viewModel.entity.get().setIntroduction(binding.etMiaoshuBeiZhu.getEditContent().trim());
        }

        if (flag == 3) {//物流业绩
            viewModel.entity.get().setLoad(binding.etDunNumber.getContent().trim());
            viewModel.entity.get().setStartDetailedAddress(binding.etStartAddress.getEditContent().trim());
            viewModel.entity.get().setEndDetailedAddress(binding.etEndAddress.getEditContent().trim());
            viewModel.entity.get().setIntroduction(binding.etMiaoshuBeiZhu.getEditContent().trim());
        }
        if (flag == 4) {
            viewModel.entity.get().setIntroduction(binding.etMiaoshuBeiZhu.getEditContent().trim());
        }

        return true;
    }

    //业绩类型
    public void setYejiTypePop(List<PickSelectEntity> list) {
        hideKeyboard(binding.tvYeJiType);
        OptionsPickerView businessOption = new OptionsPickerBuilder(WebsiteUpDataJingYingYeJiActivity.this, (options1, options2, options3, v) -> {
            flag = options1;
            binding.setFlag(flag);
            switchFlagTitle();
            String content = list.get(options1).getProjectscale();
            viewModel.entity.get().setTypeName(content);
            viewModel.entity.get().setType(options1 + 1);
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText("请选择业绩类型")
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        businessOption.setPicker(list);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    private void switchFlagTitle() {
        if (flag == 1) {
            binding.etName0.setLabelName("商品名称");
            binding.etName0.setLabelHint("请输入商品名称");
        } else if (flag == 2) {
            binding.etName0.setLabelName("租赁名称");
            binding.etName0.setLabelHint("请输入租赁名称");
        } else if (flag == 3) {
            binding.etName0.setLabelName("物流名称");
            binding.etName0.setLabelHint("请输入物流名称");
        } else {
            binding.etName0.setLabelName("项目名称");
            binding.etName0.setLabelHint("请输入项目名称");
        }
    }

    //项目状态
    public void setStatusPop(List<PickSelectEntity> list) {
        hideKeyboard(binding.tvStatus);
        OptionsPickerView businessOption = new OptionsPickerBuilder(WebsiteUpDataJingYingYeJiActivity.this, (options1, options2, options3, v) -> {
            String content = list.get(options1).getProjectscale();
            binding.tvStatus.setContent(content);
            viewModel.entity.get().setStatus(list.get(options1).getNo());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText("请选择项目状态")
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        businessOption.setPicker(list);
        businessOption.setSelectOptions(0);
        businessOption.show();
    }

    //所在地区选择
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
        if (isLoaded) {
            showCityPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }
    }

    @SuppressWarnings("unchecked")
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(WebsiteUpDataJingYingYeJiActivity.this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3 = options3;
                viewModel.entity.get().setDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                viewModel.entity.get().setDistrictName(areaName);
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;
            binding.tvArea.setContent(content);
            cityPosition1 = options1;
            cityPosition2 = options2;
            viewModel.entity.get().setProvinceName(addressEntityList.get(options1).getName());
            viewModel.entity.get().setProvinceNo(addressEntityList.get(options1).getId());
            viewModel.entity.get().setCityName(opt2.get(options1).get(options2).getName());
            viewModel.entity.get().setCityNo(opt2.get(options1).get(options2).getNo());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText("请选择项目地区")
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    //起始地
    @SuppressWarnings("unchecked")
    public void showCityPop1() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(WebsiteUpDataJingYingYeJiActivity.this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3_s = options3;
                viewModel.entity.get().setStartDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                viewModel.entity.get().setStartDistrictName(areaName);
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;
            binding.tvStartAddr.setContent(content);
            cityPosition1_s = options1;
            cityPosition2_s = options2;
            viewModel.entity.get().setStartProvinceName(addressEntityList.get(options1).getName());
            viewModel.entity.get().setStartProvinceNo(addressEntityList.get(options1).getId());
            viewModel.entity.get().setStartCityName(opt2.get(options1).get(options2).getName());
            viewModel.entity.get().setStartCityNo(opt2.get(options1).get(options2).getNo());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText("请选择项目地区")
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1_s, cityPosition2_s, cityPosition3_s);
        cityOption.show();
    }

    //目的地
    @SuppressWarnings("unchecked")
    public void showCityPop2() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(WebsiteUpDataJingYingYeJiActivity.this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3_e = options3;
                viewModel.entity.get().setEndDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                viewModel.entity.get().setEndDistrictName(areaName);
            }
            String content = addressEntityList.get(options1).getName()
                    + opt2.get(options1).get(options2).getName()
                    + areaName;
            binding.tvEndAddr.setContent(content);
            cityPosition1_e = options1;
            cityPosition2_e = options2;
            viewModel.entity.get().setEndProvinceName(addressEntityList.get(options1).getName());
            viewModel.entity.get().setEndProvinceNo(addressEntityList.get(options1).getId());
            viewModel.entity.get().setEndCityName(opt2.get(options1).get(options2).getName());
            viewModel.entity.get().setEndCityNo(opt2.get(options1).get(options2).getNo());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(getResources().getColor(R.color.color_tab_two))
                .setTitleText("请选择项目地区")
                .setTitleColor(getResources().getColor(R.color.color_tab_three)).setTitleSize(15)
                .setSubmitText("确定")
                .setSubmitColor(getResources().getColor(R.color.color_main))
                .build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1_e, cityPosition2_e, cityPosition3_e);
        cityOption.show();
    }


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_SUCCESS:
                    showCityPop();
                    isLoaded = true;
                    thread = null;
                    break;
            }
        }
    };

    private void initJsonData() {
        //城市列表
        @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
        for (int x = 0; x < addressEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            if (!TextUtils.isEmpty(viewModel.entity.get().getProvinceName()) && viewModel.entity.get().getProvinceName().equals(addressEntityList.get(x).getName())) {
                cityPosition1 = x;
                cityPosition1_s = x;
                cityPosition1_e = x;
            }
            opt2.add(addressEntityList.get(x).getChildren());
            for (int i = 0; i < addressEntityList.get(x).getChildren().size(); i++) {
                cityList.add(addressEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (addressEntityList.get(x).getChildren().get(i).getChildren() == null
                        || addressEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(addressEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        if (viewModel.entity.get() != null) {
            if (!TextUtils.isEmpty(viewModel.entity.get().getCityName()))
                for (int x2 = 0; x2 < opt2.size(); x2++) {
                    if (addressEntityList.size() > cityPosition1 && addressEntityList.get(cityPosition1).getChildren() != null && addressEntityList.get(cityPosition1).getChildren().size() > x2) {
                        if (viewModel.entity.get().getCityName().equals(addressEntityList.get(cityPosition1).getChildren().get(x2).getName())) {
                            cityPosition2 = x2;
                            cityPosition2_s = x2;
                            cityPosition2_e = x2;
                            break;
                        }
                    }
                }
            if (!TextUtils.isEmpty(viewModel.entity.get().getDistrictName()))
                for (int x3 = 0; x3 < opt3.size(); x3++) {
                    if (addressEntityList.size() > cityPosition1 &&
                            addressEntityList.get(cityPosition1).getChildren() != null && addressEntityList.get(cityPosition1).getChildren().size() > cityPosition2
                            && addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren() != null && addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().size() > x3) {
                        if (viewModel.entity.get().getDistrictName().equals(addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(x3).getName())) {
                            cityPosition3 = x3;
                            cityPosition3_s = x3;
                            cityPosition3_e = x3;
                            break;
                        }
                    }
                }
        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    // 上传文件成功回调
    public void uploadImageSuccess(String toString) {
        filePath = "";
        ArrayList<FileInfoEntity> entityArrayList = new ArrayList<>();
        FileInfoEntity entity = new FileInfoEntity();
        entity.setUrl(toString);
        entity.setName(viewModel.entity.get().getFileName());
        entityArrayList.add(entity);
        String json = new Gson().toJson(entityArrayList);
        //LogUtil.i("api", "====================" + json);
        viewModel.entity.get().setFile(json);
        viewModel.updateWebsiteJYData();
    }

    @Override
    protected void onPictureSelectorResult(List<LocalMedia> selectList) {
        filePath = selectList.get(0).getRealPath();
        binding.tvFileName.setText(FileUtil.getFileNameFromPath(filePath));
        viewModel.entity.get().setFileName(FileUtil.getFileNameFromPath(filePath));
        setViewFile(FileUtil.getFileNameFromPath(filePath), filePath);
    }


    /*
     * 返回上传数据的结果
     * */
    public void updateJYDataResult(boolean flag) {
        if (flag) {
            MToast.showToast(this, "保存成功");
            finish();
        }
    }

    /*
     * 返回经营业绩详情数据
     * */
    public void setYJDetail(WebsiteUpDataJingyingYejiEntity data) {
        binding.setViewModel(data);
        viewModel.entity.set(data);
        int type= data.getType();
        isModify = true;
        flag= type-1;
        binding.setFlag(type-1);
        if (!TextUtils.isEmpty(data.getProvinceName())) {
            binding.tvArea.setContent(data.getProvinceName() + data.getCityName() + data.getDistrictName());
        }
        if (!TextUtils.isEmpty(data.getStartProvinceName())) {
            binding.tvStartAddr.setContent(data.getStartProvinceName() + data.getStartCityName() + data.getStartDistrictName());
        }
        if (!TextUtils.isEmpty(data.getEndProvinceName())) {
            binding.tvEndAddr.setContent(data.getEndProvinceName() + data.getEndCityName() + data.getEndDistrictName());
        }

        String fileName = "";
//        if (TextUtils.isEmpty(data.getFileName())) {
//            fileName = data.getFileList().get(0).getName();
//        } else {
//            fileName = data.getFileName();
//        }
//        setViewFile(fileName, data.getFileList().get(0).getFileUrl());
        if (!TextUtils.isEmpty(data.getFileName())) {
            String json = data.getFile();
            Gson gson = new Gson();
            List<WebsiteUpDataJingyingYejiEntity.FileListBean2> list = gson.fromJson(json,
                    new TypeToken<List<WebsiteUpDataJingyingYejiEntity.FileListBean2>>() {}.getType());
            setViewFile(fileName, list.get(0).getUrl());
        }

        if (data.getCustomFields() != null) {
            binding.addView.setData(data.getCustomFields());
        }
    }

    private void setViewFile(String fileName, String realPathFromURI) {
        binding.tvFileName.setText(fileName);
        if (!TextUtils.isEmpty(fileName)) {
            int tagImage = 0;
            if (fileName.contains(".xlsx")) {
                tagImage = R.mipmap.icon_xlsx;
            } else if (fileName.contains(".jpg") || fileName.contains(".png")) {
                tagImage = R.mipmap.icon_jpg;
            } else if (fileName.contains(".pdf")) {
                tagImage = R.mipmap.icon_pdf;
            } else if (fileName.contains(".docx")) {
                tagImage = R.mipmap.icon_docx;
            }
            if (tagImage != 0) {
                binding.ivTag.setImageDrawable(getResources().getDrawable(tagImage));
            } else {
                binding.ivTag.setVisibility(View.GONE);
            }
        }
        binding.linBtnUpload.setVisibility(View.GONE);
        binding.fileLayout.setVisibility(View.VISIBLE);
    }

    public static void hideKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
